package org.shiki.hrm.filter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;

public class SmsCodeProvider implements AuthenticationProvider {

	private UserDetailsService userDetailsService;

	public UserDetailsService getUserDetailsService() {
		return userDetailsService;
	}

	public void setUserDetailsService(UserDetailsService userDetailsService) {
		this.userDetailsService = userDetailsService;
	}

	@Override
	public Authentication authenticate(Authentication authentication) throws AuthenticationException {
		// 根据手机号码去数据库查询数据
		String phone = (String) authentication.getPrincipal();
		UserDetails userDetails = userDetailsService.loadUserByUsername(phone);
		// 生成token
		SmsCodeAuthenticationToken token = new SmsCodeAuthenticationToken(userDetails.getUsername(), userDetails.getPassword(), userDetails.getAuthorities());
		token.setDetails(authentication.getDetails());
		return null;
	}

	@Override
	public boolean supports(Class<?> authentication) {
		return (SmsCodeAuthenticationToken.class.isAssignableFrom(authentication));
	}
}
